//
//  LEEHTTPRequst.swift
//  AFN请求封装
//
//  Created by 李江波 on 2016/11/13.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

import UIKit
import AFNetworking

enum method {
    case GET
    case POST
    
}

class LEEHTTPRequst: AFHTTPSessionManager {
    
    /// 声明单例
    static let HTTPShared : LEEHTTPRequst = {
    
        let httpShared = LEEHTTPRequst()
        httpShared.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return httpShared
    }()
    
    func request(method : method, urlString : String, parameters : Any?, completion : @escaping (_ response : Any?, _ error : Error?) -> ()) {
        
        let successBlock : (URLSessionDataTask , Any?) -> () = {
        
            (task , responseObject) -> () in
            completion(responseObject, nil)
        }
        
        let errorBlock : (URLSessionDataTask? , Error) -> () = { (task , error) -> () in
            
            completion(nil, error)
            print(error)
        
        }
        
        if method == .GET {
         
            self.get(urlString, parameters: parameters, progress: nil, success: successBlock, failure: errorBlock)
        }else{
        
            self.post(urlString, parameters: parameters, progress: nil, success: successBlock, failure: errorBlock)
        }
        
    }
    

}
